package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车功能
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result addShoppingCar(ShoppingCartDTO dto) {

        //先根据id查询购物车里面是否存在商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart Cart = shoppingCarMapper.getByUserId(shoppingCart);

        //如果有就修改商品数量，如果没有就添加到购物车
        if (Cart != null) {
            //如果不为空就是修改菜品数量，数量是在原来的基础上加一
            Cart.setNumber(Cart.getNumber() + 1);
            shoppingCarMapper.updateNumber(Cart);
        } else {
            //如果为空就是要添加商品到购物车
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            if (dto.getSetmealId() != null) {
                //如果为空，就是新增套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(dto.getSetmealId());

            }
            if (dto.getDishId() != null) {
                //如果不等于空，就是新增菜品
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(dto.getDishId());
                shoppingCart.setDishFlavor(dto.getDishFlavor());
            }
            //调用方法新增商品
            shoppingCarMapper.addShoppingCar(shoppingCart);

        }

        return Result.success();
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public Result getShoppingCar() {
        Long userId = BaseContext.getCurrentId();

        List<ShoppingCart> list = shoppingCarMapper.getById(userId);

        return Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Result deleteAll() {
        //获取登录用户的id
        Long userId = BaseContext.getCurrentId();
        //直接删除购物车里所有的东西
        shoppingCarMapper.deleteAll(userId);
        //返回成功
        return Result.success();
    }

    /**
     * 单独删除购物车商品
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateNumber(ShoppingCartDTO dto) {
        //新建一个对象用来存储收到的数据内容
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        //调用mapper方法用来查询购物车里的内容
        List<ShoppingCart> list = shoppingCarMapper.getById(BaseContext.getCurrentId());
        //遍历集合，判断商品数量
        list.forEach(cart -> {
            if (cart.getNumber() > 1) {
                //商品数量大于1进行数量修改
                cart.setNumber(cart.getNumber() - 1);
                shoppingCarMapper.updateNumber(cart);
            } else {
                //商品数量等于1,直接删除
                shoppingCarMapper.deleteById(cart.getId());
            }
        });

        return Result.success();
    }
}
